import unittest

from mcp_server_graph.utils import text2cypher_service


class MyTestCase(unittest.TestCase):
    def test_something(self):
        state = {"user_question": "高价值用户",
                         "selectSchema": None,  # 挑选的schema
                         "sampleCypher": None,  # 采样语句
                         "preCypher": None,  # 历史语句
                         "preCypherResult": None,  # 历史结果
                         "preReason": None,  # 业务和采样思路
                         "cypher": """// 北京市疑似诈骗用户检测
MATCH (u:User)-[:has]->(p:Phone)
WHERE p.registered_location CONTAINS '北京' 
  AND duration.between(date(p.activation_date), date()).months <= 6

WITH u, p,
  count{(p)-[c:called]-(:Phone)} AS totalCalls,
  count{(p)-[c:called]->(:Phone)} AS outgoingCalls,
  count{(p)<-[c:called]-(:Phone)} AS incomingCalls
WHERE totalCalls > 0 AND outgoingCalls * 100.0 / totalCalls > 70

MATCH (p)-[shortCall:called]-(otherPhone:Phone)
WHERE shortCall.call_duration_minutes < 0.17
  AND shortCall.roaming_type = '国际'
WITH u, p, totalCalls, outgoingCalls, incomingCalls,
  collect(shortCall) AS shortCalls,
  collect(otherPhone) AS contactedPhones,
  count(shortCall) AS shortCallCount
WHERE shortCallCount * 100.0 / totalCalls > 60
  AND (p.mobile_number STARTS WITH '170' OR p.mobile_number STARTS WITH '171')

// 构建诈骗特征画像
WITH u, p, {
  total_calls: totalCalls,
  outgoing_call_percentage: outgoingCalls * 100.0 / totalCalls,
  short_call_percentage: shortCallCount * 100.0 / totalCalls,
  in_network_months: duration.between(date(p.activation_date), date()).months,
  is_virtual_operator: (p.mobile_number STARTS WITH '170' OR p.mobile_number STARTS WITH '171'),
  international_short_calls: shortCallCount,
  contacted_phones_count: size(collect(distinct otherPhone))
} AS fraudProfile,
shortCalls, contactedPhones

// 构建子图结构
WITH u, p, fraudProfile,
  [p] + contactedPhones AS allNodes,
  shortCalls AS allRels

RETURN 
  fraudProfile as profile,
  u as red_user,
  p as red_phone,
  allNodes,
  allRels,
  [rel in allRels | rel] as orange_calls
LIMIT 3""",
                         "reason": """基于北京市疑似诈骗用户的分析需求，业务思路如下：
1. 筛选在网时长较短的用户（<6个月），新号风险较高
2. 分析通话行为特征：短时长通话（<10秒）占比超过60%，主叫比例超过70%
3. 重点关注国际漫游通话记录
4. 识别虚拟运营商号码（170/171开头）
5. 构建诈骗特征画像，包括总通话数、主叫比例、短通话比例、在网月数等
6. 用红色高亮标记疑似诈骗用户节点（red_user），用橙色高亮标记异常通话关系（orange_call）
7. 返回完整的子图结构，包含用户、手机号码及相关通话记录""",
                         "query_result": None,
                         "error": """{code: Neo.ClientError.Statement.SyntaxError} {message: Variable `otherPhone` not defined (line 30, column 49 (offset: 1259))
"  contacted_phones_count: size(collect(distinct otherPhone))"
                                                 ^}""",
                         "retry_count": 0,
                         "max_retries": 10,
                         "interpretation": None
                         }
        text2cypher_service.fix_cypher(state)


if __name__ == '__main__':
    unittest.main()
